package com.zlzxm.shoesmarket.viewmodel;

import android.text.Html;
import android.text.Spanned;
import android.util.Log;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.zlzxm.shoesmarket.net.RequestTip;
import com.zlzxm.shoesmarket.net.repository.UserRepository;
import com.zlzxm.shoesmarket.net.respone.BaseResponse;
import com.zlzxm.shoesmarket.net.respone.RpVip;
import com.zlzxm.shoesmarket.util.RxAndroidHelp;
import com.zlzxm.shoesmarket.view.adapter.ADVipInfo;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.rxjava3.functions.Consumer;

/**
 * CREATE BY zlzxm ON 4/13/21
 */
public class VMVipInfo extends ViewModel {

    private static final String TAG = "VMVipInfo";

    UserRepository userRepository = new UserRepository();

    private MutableLiveData<List<ADVipInfo.EntityVipInfo>> liveVipInfo = new MutableLiveData<>();

    private MutableLiveData<String> liveErrorToast = new MutableLiveData<>();

    private MutableLiveData<Spanned>  liveVipDesc= new MutableLiveData<>();

    public MutableLiveData<Spanned> getLiveVipDesc() {
        return liveVipDesc;
    }

    public MutableLiveData<String> getLiveErrorToast() {

        return liveErrorToast;
    }

    public MutableLiveData<List<ADVipInfo.EntityVipInfo>> getLiveVipInfo() {
        return liveVipInfo;
    }

    public void requestVipInfo(){

        userRepository.vipList()
                .compose(RxAndroidHelp.turn())
                .subscribe(new Consumer<BaseResponse<List<RpVip>>>() {
                    @Override
                    public void accept(BaseResponse<List<RpVip>> response) throws Throwable {

                        if(response.getCode() == 0){


                            liveVipInfo.setValue(createData(response.getData()));

                        }



                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Throwable {

                        Log.e(TAG,throwable.getMessage());
                        liveErrorToast.setValue(RequestTip.REQUEST_ERROR);
                    }
                });
    }

    public void requestVipDesc(){

        userRepository.vipDesc()
                .compose(RxAndroidHelp.turn())
                .subscribe(new Consumer<BaseResponse<String>>() {
                    @Override
                    public void accept(BaseResponse<String> response) throws Throwable {

                        if(response.getCode() == 0){

                            liveVipDesc.setValue(Html.fromHtml(response.getData()));

                        }



                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Throwable {

                        Log.e(TAG,throwable.getMessage());
                        liveErrorToast.setValue(RequestTip.REQUEST_ERROR);
                    }
                });

    }


    //<editor-fold defaultState="collapsed" desc="测试">


    public List<ADVipInfo.EntityVipInfo> createData(List<RpVip> vips){

        List<ADVipInfo.EntityVipInfo> list = new ArrayList<>();

        for (RpVip vip : vips) {


            String desc = vip.getSale() / vip.getMonth() + "元/月";

            ADVipInfo.EntityVipInfo entityVipInfo=   new ADVipInfo.
                    EntityVipInfo(vip.getName(),vip.getSale()+"",vip.getPrice(),desc);

            list.add(entityVipInfo);
        }

        return list;
    }


    // </editor-fold>


}
